﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;

using System.IO;
using System.Net;
using Utilities;

using Google.GData.Client; 

namespace ContentProcessor
{
    public class TwitterContent : WebContent
    {

        #region WebContent Members

        public void Search(string keywords, List<ResultItem> results)
        {
            string TwitterSearchAPI = "http://search.twitter.com/search.atom";

            string QueryURL = TwitterSearchAPI + "?" +
                                       "q=" + System.Security.SecurityElement.Escape(keywords);
            try
            {
                XPathNavigator nav;
                XPathDocument docNav;

                HttpWebRequest loginRequest = (HttpWebRequest)System.Net.WebRequest.Create(QueryURL);

                loginRequest.Credentials = CredentialCache.DefaultCredentials;
                loginRequest.Proxy = Google.GData.Client.Utilities.CommonOperations.GetProxy();
                loginRequest.Method = "GET";

                HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();

                if (loginResponse.Headers.GetValues("Status")[0].Equals("200 OK"))
                {
                    Stream receiveStream = loginResponse.GetResponseStream();
                    docNav = new XPathDocument(receiveStream);
                    nav = docNav.CreateNavigator();

                    XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable);
                    manager.AddNamespace("xmlns:google" , "http://base.google.com/ns/1.0");
                    manager.AddNamespace("xmlns:openSearch" , "http://a9.com/-/spec/opensearch/1.1/");
                    //manager.AddNamespace("xmlns" , "http://www.w3.org/2005/Atom");
                    manager.AddNamespace("xmlns:twitter" , "http://api.twitter.com/");

                    //nav.MoveToRoot();
                    XPathNodeIterator entries = nav.Select(@"/*/*");
                    foreach (XPathNavigator entry in entries)
                    {
                        if (entry.Name == "entry")
                        {
                            ResultItem item = new ResultItem();
                            //item.URL = entry.SelectChildren(""
                            XPathNodeIterator nodes;

                            nodes = entry.SelectChildren("link", "http://www.w3.org/2005/Atom");
                            foreach (XPathNavigator n in nodes)
                            {
                                if (n.GetAttribute("type", "") == "text/html")
                                {
                                    item.URL = n.GetAttribute("href", "");
                                    item.DisplayURL = n.GetAttribute("href", "");
                                }
                            }
                            nodes = entry.SelectChildren("content", "http://www.w3.org/2005/Atom");
                            foreach (XPathNavigator n in nodes)
                            {
                                //int i = 100;
                                item.Description = (string)n.TypedValue; 
                            }
                            nodes = entry.SelectChildren("title", "http://www.w3.org/2005/Atom");
                            foreach (XPathNavigator n in nodes)
                            {
                                //int i = 100;
                                item.Title = (string)n.TypedValue;
                            }

                            results.Add(item); 
                        }
                    }
                    //ParseResults(readStream, results);
                    receiveStream.Close();

                    //string s = readStream.ReadContentAsString();
                    //Console.Write(readStream.ReadToEnd());
                }
                loginResponse.Close();
            }
            catch (WebException ex)
            {
                System.Diagnostics.Trace.WriteLine("SearchDirect error: " + ex.StackTrace);
            }
        }

        #endregion
    }
}
